<template>
  <div>
    <BasicTable
      @register="registerTable"
      :can-resize="true"
      :bordered="true"
      :striped="true"
      :loading="false"
      size="small"
    >
      <template #statusFlag="{ record }">
        <Tag :color="record.success ? 'green' : 'red'">{{ record.status }}</Tag>
      </template>
      <template #expiredtm="{ record }">
        <span :style="{ color: record.hasexpired ? 'black' : 'green' }">{{
          record.expiredtm
        }}</span>
      </template>
      <template #ip="{ record }">
        <a :href="`https://www.baidu.com/s?wd=${record.ip}`" target="_blank">{{ record.ip }}</a>
      </template>
      <template #token="{ record }">
        <span @click="copyText(record.token)">{{ record.token }}</span>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:eye-line',
              label: '详情',
              onClick: onLoginlogDetail.bind(null, record),
            },
            {
              icon: 'ant-design:field-time-outlined',
              label: '设置Token过期',
              color: 'warning',
              ifShow: record.hasexpired == false && record.token,
              popConfirm: {
                title: '是否确认设置过期',
                placement: 'left',
                confirm: onSetTokenExpired.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <LoginLogDetail @register="registerLoginLogDetailForm" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { Tag } from 'ant-design-vue';
  import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
  const { clipboardRef } = useCopyToClipboard();
  import { queryLoginLog, setTokenExpired } from '/@/api/platform/log';
  import { loginLogTableColumns, loginLogQueryConditionFormSchema } from './log.data';

  //引入子组件
  import LoginLogDetail from './LoginLogDetail.vue';

  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';

  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  const [registerLoginLogDetailForm, { openModal: openLoginLogDetailForm }] = useModal();

  export default defineComponent({
    name: 'Loginlog', //当前组件名称
    components: { BasicTable, TableAction, LoginLogDetail, Tag },
    setup() {
      //定义模块的名称，提供给子孙组件获取使用
      const moduleName = ref('登录日志');
      /**
       * 请求后台查询接口返回分页数据显示
       */
      const [registerTable, { reload }] = useTable({
        title: '登录日志管理',
        api: queryLoginLog, //api中指定的为table加载数据对应的查询方法
        columns: loginLogTableColumns, //表头数据
        formConfig: {
          labelWidth: 110, //设置查询表单中label的宽度
          schemas: loginLogQueryConditionFormSchema, // form查询表单
          showResetButton: false, //查询条件部分中是否显示重置按钮
        },
        pagination: {
          pageSizeOptions: ['10', '20', '50', '100', '200'], //分页组件中每页显示条数的下拉框值
        },
        useSearchForm: true,
        showTableSetting: true, //是否显示表格设置
        bordered: true,
        showIndexColumn: true,
        actionColumn: {
          width: 180,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      /**
       * 打开Api日志详情窗口
       * @param record 行数据
       */
      function onLoginlogDetail(record: Recordable) {
        //使用props传递数据至子组件，在data参数中指定
        let propsData: QcUnifyTransToChildProps = {
          moduleName: moduleName.value,
          operateType: OperationTypeEnum.CUSTOME,
          operateString: '查看详情',
          dataId: record.id,
        };
        openLoginLogDetailForm(true, propsData);
      }

      /**
       * 设置Token过期
       */
      function onSetTokenExpired(record) {
        setTokenExpired(record.token).then((res) => {
          afterSuccess();
        });
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }

      function copyText(text) {
        clipboardRef.value = text;
        createMessage.success('复制成功');
      }

      return {
        registerTable,
        registerLoginLogDetailForm,
        onLoginlogDetail,
        onSetTokenExpired,
        afterSuccess,
        copyText,
      };
    },
  });
</script>
